#include "input.h"

void init_input() {

}

void cleanup_input(button_node *root) {
    destroy_buttons(root);
}

button construct_button(char id, int x, int y, int w, int h, void (*callback)(void* data), SDL_Texture *texture) {
    button button;

    button.id = id;
    button.x = x;
    button.y = y;
    button.w = w;
    button.h = h;
    button.callback = callback;
    button.texture = texture;

    return button;
}

void register_mousedown_event(button_node **root, button_node **tail, button b) {
    /* special case root node */
    if (*root == NULL) {
        *root = malloc(sizeof(button_node));
        if (*root == NULL) {
            fprintf(stderr, "error allocating memory for button_node\n");
            exit(EXIT_FAILURE);
        }

        /* set values */
        (*root)->payload = b;
        (*root)->next = NULL;

        /* set tail */
        *tail = *root;
        return;
    }

    (*tail)->next = malloc(sizeof(button_node));
    (*tail) = (*tail)->next;
    (*tail)->payload = b;
    (*tail)->next = NULL;
}

void destroy_buttons(button_node *root) {
    button_node *node = root;
    while (node != NULL) {
        button_node *current = node;
        node = node->next;
        /* free all resources of button */
        if (current->payload.texture != NULL)
            SDL_DestroyTexture(current->payload.texture);
        free(current);
    }
}

void get_input(button_node *root) {
    SDL_Event event;

    while (SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_QUIT:
                exit(0);
                break;

            case SDL_KEYDOWN:
                switch (event.key.keysym.sym) {
                    case SDLK_ESCAPE:
                        if (event.key.keysym.mod & KMOD_CTRL)
                            exit(0);
                        break;
                    case SDLK_q:
                        if (event.key.keysym.mod & KMOD_CTRL)
                            exit(0);
                        break;
                    case SDLK_F4:
                        if (event.key.keysym.mod & KMOD_ALT)
                            exit(0);
                        break;
                    case SDLK_w:
                        break;
                    case SDLK_a:
                        break;
                    case SDLK_s:
                        break;
                    case SDLK_d: {
                        break;
                    }
                    default:
                        break;
                }
                break;

            case SDL_MOUSEBUTTONDOWN: {
                /* query mouse press */
                int x, y;
                SDL_GetMouseState(&x, &y);
                if (DEBUG)
                    printf("mouse button down %i %i\n", x, y);

                /* iterate buttons to handle possible event */
                button_node *node = root;
                while (node != NULL) {
                    button_node *current = node;
                    node = node->next;
                    if (DEBUG)
                        printf("button { %i, %i, %i, %i, %p, %p } @ %p\n", current->payload.x, current->payload.y, current->payload.w, current->payload.h, current->payload.callback, current->payload.texture, current);
                    /* collision detection lol */
                    if (x >= current->payload.x &&
                        y >= current->payload.y &&
                        x < current->payload.x + current->payload.w &&
                        y < current->payload.y + current->payload.h) {
                        (*current->payload.callback)(NULL);
                    }
                }
                break;
            }

            default:
                break;
        }
    }
}